﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LinqToDB;

namespace Winfrom_Login_Test_0001
{
    public partial class LoginSuccess : Form
    {
        public LoginSuccess()
        {
            InitializeComponent();
        }


        public AddForm(int id, string name, double UpTime, double cTime)
        {
            id = id;            
        }

        private void LoginSuccess_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“admin001DataSet.Users”中。您可以根据需要移动或删除它。
            this.usersTableAdapter.Fill(this.admin001DataSet.Users);

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

            string cmd = "select * from Users";

            var dt = DsHelper.GetDataTable(cmd);

            dataGridView1.DataSource = dt;

            dataGridView1.ReadOnly = true; //只读

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; //单机标头或该行所的单元格，选择整行

            dataGridView1.AllowUserToAddRows = false;  //删除新行


        }

        //查找
        private void button2_Click(object sender, EventArgs e)
        {
            var name = textBox1.Text;

            var sql = string.Format("select * from Users Where Username like '%{0}%'", name);

            var dt = DsHelper.GetDataTable(sql);

            dataGridView1.DataSource = dt; //获取表数据


        }

        //添加
        private void button3_Click(object sender, EventArgs e)
        {

            AddForm addForm = new AddForm();
            addForm.Show();


        }
        //更新
        private void button4_Click(object sender, EventArgs e)
        {
            AddForm addform = new AddForm(id,name,cTime,UpTime);
            addform.Show();

            if (dataGridView1.SelectedRows.Count > 0)
            {
                var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value; //更新后只显示更新后的一行数据，不会罗列所有数据
                var name = (string)dataGridView1.SelectedRows[0].Cells["Username"].Value;
                var cTime = (double)dataGridView1.SelectedRows[0].Cells["CreatedTime"].Value;
                var UpTime = (double)dataGridView1.SelectedRows[0].Cells["UpdatedTime"].Value;
            }
            else
            {
                MessageBox.Show("请选择数据行进行更新","提示");
            }


        }
        //删除
        private void button5_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var row = dataGridView1.SelectedRows[0];

                var user = (int)row.Cells["id"].Value;

                var res = MessageBox.Show("确定删除？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

                if (res.Equals(DialogResult.Yes))
                {
                    var sql = string.Format("delect * from Users Where Id like %{0}%", user);//删除后不罗列所有数据

                    var rowCount = DsHelper.AddorUpdateorDel(sql);
                    if (rowCount > 0)
                    {
                        MessageBox.Show("删除成功！", "提示");
                    }
                }
            }
            else
            {
                MessageBox.Show("请选择行", "提示");
            }


        }




        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
}







